#孙晨
import csv
import urllib.request
from bs4 import BeautifulSoup
import time


url = 'http://hotels.ctrip.com/events/shanghai2/xinkaiyejiudianjianianhua.html?page=' 

csvFile = open("csvFile/file.csv", 'wt', newline='\n', encoding='utf-8')  #newline换行符
writer = csv.writer(csvFile)

for i  in [1, 2, 3]:  #这里的  range（初始，结束，间隔）
	# print (i)
    #urllib.request库用来向该网服务器发送请求，请求打开该网址链接
    html = urllib.request.urlopen('http://hotels.ctrip.com/events/shanghai2/xinkaiyejiudianjianianhua.html?page=%d' % i)    
    #BeautifulSoup库解析获得的网页，第二个参数一定记住要写上‘lxml’，记住就行
    bsObj = BeautifulSoup(html,'html.parser')  

    print('==============' + '第%d页'%i+'==============')
    h3_node_list = bsObj.find_all('h3',{'class':"hotelName"}) 
    p_node_list = bsObj.find_all('p',{'class':"hotel_location"}) 
    comm_node_list = bsObj.find_all('span',{'class':"comm"}) 
    star_node_list = bsObj.find_all('span',{'class':"star"})
    prz_node_list = bsObj.find_all('p',{'class':"prz cf"})
    for i in list(range(len(h3_node_list))):
    	csvRow = []
    	hotelname =h3_node_list[i]
    	hotel=p_node_list[i]
    	comm=comm_node_list[i]
    	star=star_node_list[i]
    	prz =prz_node_list[i]
    	csvRow.append(hotelname.get_text())
    	csvRow.append(hotel.get_text())
    	csvRow.append(comm.get_text())
    	csvRow.append(star.get_text())
    	csvRow.append(prz.get_text())
    	writer.writerow(csvRow)
csvFile.close()
